#! /usr/local/bin/ruby
$KCODE = 'utf8'
require 'net/http'
require 'net/https'

# Check the server
def link_https?(server,path, username, password)
  http = Net::HTTP.new(server,443)
  req = Net::HTTP::Get.new(path)
  #https
  http.use_ssl = true
  req.basic_auth username, password
  begin
    http.request(req)
    return true
  rescue SocketError
    return false
  end
end

# run hg *args
def hg(args)
  targs=['hg']
  args.each do |a|
    targs<<'"'+a.gsub('"','\"')+'"'
  end
  cmd = targs.join(' ')
  system cmd
end

# For google code
USER = ARGV[1]
PASSWD = ARGV[2]
GOOGLE_SERVER=ARGV[3]
GOOGLE_PATH='/hg/'
DEST_GOOGLE='https://'+USER+':'+PASSWD+'@'+GOOGLE_SERVER+GOOGLE_PATH

ci = ['commit','-m']+[ARGV[0]]
hg ci

# push to google code
if link_https?(GOOGLE_SERVER,GOOGLE_PATH,USER, PASSWD)
  ci = ['push', DEST_GOOGLE]
  hg ci
end

